package riddles.content;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class RiddlesDbAdapter {
	public static final String KEY_ROWID = "_id";
	public static final String RIDDLESID = "riddlesid";
	public static final String QUESTION = "question";
	public static final String ANSWER = "answer";
	public static final String GETTIME = "gettime";
	public static final String RANK = "rank";
	public static final String SENDTIMES = "sendtimes";
	private DatabaseHelper mDbHelper;
	private SQLiteDatabase mDb;
	private static final String DATABASE_NAME = "happydatabaseriddles";
	private static final String DATABASE_TABLE = "riddles_content";
	private static final int DATABASE_VERSION = 1;
	private final Context mCtx;
	private static final String DATABASE_CREATE = "create table "
			+ DATABASE_TABLE + " (_id integer primary key autoincrement, "
			+ RIDDLESID + " text, " + QUESTION + " text, " + ANSWER
			+ " text , " + GETTIME + " text , " + RANK + " text ," + SENDTIMES
			+ " text );";

	public RiddlesDbAdapter(Context ctx) {
		this.mCtx = ctx;
	}

	public RiddlesDbAdapter open() throws SQLException {
		mDbHelper = new DatabaseHelper(mCtx);
		mDb = mDbHelper.getWritableDatabase();
		return this;
	}

	public void closeclose() {
		mDbHelper.close();
	}

	public boolean deleteDiary(String riddlesid) {

		return mDb.delete(DATABASE_TABLE, RIDDLESID + "='" + riddlesid + "'",
				null) > 0;
	}

	public long insertContent(String riddlesid, String question, String answer) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(RIDDLESID, riddlesid);
		initialValues.put(QUESTION, question);
		initialValues.put(ANSWER, answer);
		deleteDiary(riddlesid);// 删除重复数据
		return mDb.insert(DATABASE_TABLE, null, initialValues);
	}

	public Cursor getAllNotes() {

		return mDb
				.query(DATABASE_TABLE, new String[] { KEY_ROWID, RIDDLESID,
						QUESTION, ANSWER }, null, null, null, null, KEY_ROWID
						+ " desc");
	}

	public Cursor getContent(long rowId) throws SQLException {

		Cursor mCursor = mDb.query(true, DATABASE_TABLE, new String[] {
				KEY_ROWID, RIDDLESID, QUESTION, ANSWER }, KEY_ROWID + "="
				+ rowId, null, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;

	}

	public static class DatabaseHelper extends SQLiteOpenHelper {
		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		public DatabaseHelper(Context context, String name,
				CursorFactory factory, int version) {
			super(context, name, factory, version);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(DATABASE_CREATE);

		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
			db.execSQL("DROP TABLE IF EXISTS " + DATABASE_NAME);
			onCreate(db);
		}

	}

	public Cursor getNextContent(long rowId) throws SQLException {
		Cursor mCursor = mDb.query(DATABASE_TABLE, new String[] { KEY_ROWID,
				RIDDLESID, QUESTION, ANSWER }, KEY_ROWID + "<" + rowId, null,
				null, null, KEY_ROWID + " desc ");
		return mCursor;
	}

	public Cursor getPreContent(long rowId) throws SQLException {
		Cursor mCursor = mDb.query(DATABASE_TABLE, new String[] { KEY_ROWID,
				RIDDLESID, QUESTION, ANSWER }, KEY_ROWID + ">" + rowId, null,
				null, null, KEY_ROWID + " asc");
		return mCursor;

	}
}
